home *** CD-ROM | disk | FTP | other *** search
/ ASP Advantage 1993 / The Association of Shareware Professionals Advantage CD-ROM 1993.iso / files / windions / we_20n / winedit / weextsrc.zi_ / WE_EXT.C < prev    next >
C/C++ Source or Header  |  1993-01-13  |  14KB  |  421 lines

  1. /*-------------------------------------------------------------------------*\
  2.  |                                                                         |
  3.  |                                                                         |
  4.  |  WE_EXT.C - A Sample DLL Extension Processor for WinEdit                |
  5.  |                                                                         |
  6.  |                                                                         |
  7. \*-------------------------------------------------------------------------*/
  8. #define STRICT
  9. #define _WINDLL
  10. #include <windows.h>
  11. #include "we_ext.h"
  12. #include "private.h"
  13. #include <string.h>
  14.  
  15. #define NOREF(a)  {a=a;}
  16.  
  17. HMENU hTrackMenu;
  18. BOOL bWait=TRUE;
  19. BOOL bCapture=TRUE;
  20. char szCommand[256];
  21.  
  22. /*
  23.  * JLD-12/9/92 Added this to setup the default GREP string, which recurs if
  24.  * no work is under the cursor.  See line 204.
  25.  */
  26.  
  27. char GLOB_GrepCmd[256] = {"tee.com fgrep.com -M %s *.c"};
  28.  
  29. /*-------------------------------------------------------------------------*\
  30.  |                                                                         |
  31.  |  Function:   WE_ExtensionProc                                           |
  32.  |                                                                         |
  33.  |  Purpose:    WinEdit calls this function with the WEN_* messages and    |
  34.  |              whenever a user-defined menu item or accelerator is        |
  35.  |              accessed.                                                  |
  36.  |                                                                         |
  37.  |  Parameters: HWND hWnd   - WinEdit's window handle                      |
  38.  |                                                                         |
  39.  |              UINT wParam - Message ID.  If wParam is >= WE_EXTFIRST,    |
  40.  |                            the DLL is being requested to perform the    |
  41.  |                            user-defined action.                         |
  42.  |                                                                         |
  43. \*-------------------------------------------------------------------------*/
  44. UINT FAR PASCAL WE_ExtensionProc(HWND hWnd,     /* WinEdit's window handle */
  45.                                  HANDLE hInst,  /* instance identifier     */
  46.                                  UINT wParam,   /* command ID              */
  47.                                  LONG lParam)   /* additional information  */
  48.    {
  49.  
  50.    switch (wParam)
  51.       {
  52.  
  53.       case WEN_LOADMENU:
  54.  
  55.          /*  This is the menu WinEdit will display when there
  56.           *  is at least one document window open.  Return NULL
  57.           *  to use the default WinEdit menu.
  58.           *
  59.           */
  60.  
  61.          return (UINT)LoadMenu(hInst, "MyMenu");
  62.          break;
  63.  
  64.       case WEN_LOADSHORTMENU:
  65.  
  66.          /*  this is the menu WinEdit will display when there
  67.              *  are no document windows open.  Return NULL
  68.              *  to use the default WinEdit menu.
  69.              *
  70.              */
  71.    
  72.             return (UINT)LoadMenu(hInst, "MyShortMenu");
  73.             return NULL;
  74.             break;
  75.    
  76.       case WEN_LOADACCELS:
  77.  
  78.          /*  To re-define the WinEdit command keys, load your
  79.           *  own accelerator table here.  Return NULL to
  80.           *  use the default WinEdit accelerators.
  81.           *
  82.           */
  83.  
  84.          return (UINT)LoadAccelerators (hInst,"MyAccels");
  85.          break;
  86.  
  87.       case WEN_GETWINDOWMENU:
  88.  
  89.          /*  WinEdit needs the handle of the submenu to
  90.           *  append MDI document names to.  The hWnd parameter
  91.           *  is used to send the handle to the main menu.
  92.           *  This message will not be sent if you return
  93.           *  NULL to the WEN_LOADMENU message.
  94.           */
  95.          return (UINT)GetSubMenu ((HMENU)hWnd, WINDOWMENU);
  96.          break;
  97.  
  98.       case WEN_GETMACROMENU:
  99.  
  100.          /*  WinEdit needs the handle of the submenu to
  101.           *  append macro names to.  The hWnd parameter
  102.           *  is used to send the handle to the main menu.
  103.           *  This message will not be sent if you return
  104.           *  NULL to the WEN_LOADMENU message.
  105.           */
  106.          return (UINT)GetSubMenu ((HMENU)hWnd, MACROMENU);
  107.          break;
  108.  
  109.          /*  WinEdit sends right button clicks to the
  110.           *  extension processor.  lParam contains the
  111.           *  x and y coordinates of the mouse cursor,
  112.           *  in screen coordinates.
  113.           */
  114.       case WEN_RBUTTONDOWNS:
  115.          edHelpKeyWord(hWnd);
  116.          break;
  117.  
  118.       case WEN_RBUTTONDOWN:
  119.  
  120.          if (!hTrackMenu)
  121.             {
  122.  
  123.             hTrackMenu = CreatePopupMenu();
  124.             if (!hTrackMenu)
  125.                break;
  126.  
  127.  /* JLD 12/9/92 - Changed these labels to the Ctrl-V,X,Z, etc. which matches the 
  128.   *               current pull-down labels.
  129.   *               Also changed F3 to Previous error, F4 to next error, which seems
  130.   *               more consistent.  I left Re-Do as Ctrl-Backspace. 
  131.   *               Added Close to this menu.
  132.   *               Added Ctrl L as a synonym for Ctrl-F5.  (similar to Wordstar)
  133.   */ 
  134.                
  135.             AppendMenu(hTrackMenu,MF_STRING,IDM_FILEOPEN,"&Open...\tF3");
  136.             AppendMenu(hTrackMenu,MF_STRING,IDM_FILESAVE,"&Save\tF2");                 
  137.             AppendMenu(hTrackMenu,MF_STRING,IDM_WINDOWCLOSE,"&Close\tCtrl+F4");                
  138.             AppendMenu(hTrackMenu,MF_STRING,IDM_FILEPRINT,"&Print\tF9");                
  139.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  140.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITUNDO,"&Undo\tCtrl+Z");           
  141.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITREDO,"&Redo\tCtrl+BkSp");          
  142.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  143.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITCUT,"Cu&t\tCtrl+X");     
  144.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITCOPY,"&Copy\tCtrl+C");            
  145.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITPASTE,"&Paste\tCtrl+V");          
  146.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  147.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHFIND,"&Find...\tF5");              
  148.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHNEXT,"&Repeat Last Find\tCtrl+F5|L");
  149.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHCHANGE,"&Change...\tF6");            
  150.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  151.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHPREVERR,"&Previous Error\tShift+F3");    
  152.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHNEXTERR,"&Next Error\tShift+F4");
  153.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  154.             AppendMenu(hTrackMenu,MF_STRING,IDM_HELPKEYWORDS,"&Key Word Help\tShift+F1");  
  155.             }
  156.             
  157.          if (hTrackMenu)
  158.             {
  159.             POINT pt;
  160.  
  161.             pt = MAKEPOINT(lParam);
  162.             TrackPopupMenu(hTrackMenu,0,pt.x-10,pt.y-6,0,hWnd,0L);
  163.             }
  164.             
  165.          return TRUE;   
  166.          break;
  167.  
  168.       case WEN_END:
  169.  
  170.          /*  WinEdit is shutting down.  Do any clean-up processing
  171.           *  here.
  172.           */
  173.          if (hTrackMenu)
  174.             {
  175.             DestroyMenu(hTrackMenu);
  176.             hTrackMenu = (HMENU)NULL;
  177.             }
  178.          return TRUE;
  179.          break;
  180.  
  181.       case WEN_INITMENU:
  182.  
  183.          /*  This message is sent before showing any drop down
  184.           *  menu items.  Respond by setting any checkmarks,
  185.           *  graying any inapplicable items, etc.
  186.           *
  187.           */
  188.          return InitMenu(hWnd);
  189.          break;
  190.  
  191.  
  192.       /*  You can define your own commands in the range
  193.        *  WE_EXTFIRST to WE_EXTLAST that can be attached to
  194.        *  menu items or accelerators.
  195.        */
  196.  
  197.       case EXT_GREP:
  198.          {
  199.          char szWord[64];
  200.          edEditGetCurrentWord(hWnd,szWord,63);
  201.          if (szWord[0])
  202.             wsprintf(szCommand,"tee.com fgrep.com -M %s *.c",(LPSTR)szWord);
  203.          else
  204.             wsprintf(szCommand,GLOB_GrepCmd);  /* JLD 12/9/92 see beginning of file */
  205.  
  206. /* JLD 12/9/92 this *remembers* the last string if no word is selected */
  207.             
  208.          if (DialogBox(hInst,"CommandBox",hWnd,CommandDlgProc))
  209.             edRunCommand(hWnd, bWait, bCapture, szCommand);
  210.          strcpy(GLOB_GrepCmd,szCommand);  /* JLD 12/9/92 save current into the GLOB var */
  211.          return TRUE;
  212.          break;
  213.          }
  214. /* JLD 12/9/92 the following now provide braces for if and for, similar to the switch */
  215.  
  216.       case EXT_IF:
  217.          {
  218.          /* Ctrl+I:  'C' template for   if (  )
  219.                                         {
  220.                                         
  221.                                         }
  222.           */
  223.          UINT wColNo;
  224.          UINT wLineNo;
  225.  
  226.          edEditInsertString(hWnd,"if (  )\r{\r\r}");
  227.  
  228.          wColNo  = edGetColumnNumber(hWnd);
  229.          wLineNo = edGetLineNumber(hWnd);
  230.  
  231.          edEditGoToColumn(hWnd,wColNo+4);
  232.  
  233.          edEditGoToLine(hWnd,wLineNo-3);
  234.  
  235.          return TRUE;
  236.          break;
  237.          }
  238.  
  239.  
  240.       case EXT_FOR:         
  241.          {
  242.          /* Ctrl+F:  'C' template for   for ( ; ; )
  243.  
  244.           */
  245.          UINT wColNo;
  246.          UINT wLineNo;
  247.  
  248.          edEditInsertString(hWnd,"for ( ; ; )\r{\r\r}");
  249.  
  250.          wColNo  = edGetColumnNumber(hWnd);
  251.          wLineNo = edGetLineNumber(hWnd);
  252.  
  253.          edEditGoToColumn(hWnd,wColNo+5);
  254.  
  255.          edEditGoToLine(hWnd,wLineNo-3);
  256.  
  257.  
  258.          return TRUE;
  259.          break;
  260.          }
  261.  
  262.       case EXT_SWITCH:
  263.          {
  264.          /* Ctrl+S:  'C' template for   switch (  )
  265.                                            {
  266.  
  267.                                            }
  268.           */
  269.          UINT wColNo;
  270.          UINT wLineNo;
  271.  
  272.          edEditInsertString(hWnd,"switch (  )\r   {\r\r}");
  273.  
  274.          wColNo  = edGetColumnNumber(hWnd);
  275.          wLineNo = edGetLineNumber(hWnd);
  276.  
  277.          edEditGoToColumn(hWnd,wColNo+5);
  278.  
  279.          edEditGoToLine(hWnd,wLineNo-3);
  280.  
  281.          return TRUE;
  282.          break;
  283.          }
  284.  
  285.       default:
  286.  
  287.          /* return NULL to all messages not processed. */
  288.  
  289.          break;
  290.  
  291.       }  /* end switch (wParam) */
  292.  
  293.    return NULL;
  294.  
  295.    }
  296.  
  297. BOOL FAR PASCAL LibMain(HANDLE hInstance, UINT wDataSeg, UINT cbHeap, LPSTR lpszCmdLine)
  298.    {
  299.    return TRUE;
  300.  
  301.    NOREF(hInstance);
  302.    NOREF(wDataSeg);
  303.    NOREF(cbHeap);
  304.    NOREF(lpszCmdLine);
  305.    }
  306.  
  307.  
  308. int FAR PASCAL WEP(int nParameter)
  309.    {
  310.    return TRUE;
  311.  
  312.    NOREF(nParameter);
  313.    }
  314.  
  315.  
  316. BOOL FAR PASCAL  CommandDlgProc(HWND hDlg, UINT msg, UINT wParam, LONG lParam)
  317.    {
  318.    switch (msg)
  319.       {
  320.       case WM_INITDIALOG:
  321.          CheckDlgButton(hDlg,IDD_WAIT,bWait);
  322.          CheckDlgButton(hDlg,IDD_CAPTURE,bCapture);
  323.          SetDlgItemText(hDlg,IDD_COMMAND,szCommand);
  324.  
  325.       case WM_COMMAND:
  326.          switch(wParam)
  327.             {
  328.             case IDOK:
  329.                bWait = IsDlgButtonChecked(hDlg,IDD_WAIT);
  330.                bCapture = IsDlgButtonChecked(hDlg,IDD_CAPTURE);
  331.                if (GetDlgItemText(hDlg,IDD_COMMAND,szCommand,sizeof(szCommand)))
  332.                   {
  333.                   EndDialog(hDlg,TRUE);
  334.                   break;
  335.                   }
  336.                /* else fall through */
  337.  
  338.             case IDCANCEL:
  339.                EndDialog(hDlg,FALSE);
  340.                break;
  341.  
  342.             default:
  343.                return (FALSE);
  344.             }
  345.          break;
  346.  
  347.       default:
  348.          return(FALSE);
  349.       }
  350.    return (TRUE);
  351.  
  352.    NOREF(lParam);
  353.    }
  354.  
  355.  
  356. int InitMenu(HWND hWnd)
  357.    {
  358.    UINT wStatus;
  359.    HMENU hCurrentMenu;
  360.    POINT ptStart,ptEnd;
  361.  
  362.    hCurrentMenu = GetMenu(hWnd);
  363.  
  364.    /*  if there is a current selection, enable the cut & copy
  365.     *  commands.
  366.     */
  367.    wStatus = (UINT)edGetSelectionState(hWnd, &ptStart, &ptEnd);
  368.    if (!wStatus)
  369.       wStatus = MF_GRAYED;
  370.    else
  371.       wStatus = MF_ENABLED;
  372.    EnableMenuItem(hCurrentMenu, IDM_EDITCUT,  wStatus);
  373.    EnableMenuItem(hCurrentMenu, IDM_EDITCOPY, wStatus);
  374.  
  375.    /*  if there is text on the clipboard, enable the paste
  376.     *  command.
  377.     */
  378.    if (OpenClipboard(hWnd))
  379.       {
  380.       if (IsClipboardFormatAvailable(CF_TEXT)
  381.             || IsClipboardFormatAvailable(CF_OEMTEXT))
  382.          EnableMenuItem(hCurrentMenu, IDM_EDITPASTE, MF_ENABLED);
  383.       else
  384.          EnableMenuItem(hCurrentMenu, IDM_EDITPASTE, MF_GRAYED);
  385.       CloseClipboard();
  386.       }
  387.    else
  388.       EnableMenuItem(hCurrentMenu, IDM_EDITPASTE, MF_GRAYED);
  389.  
  390.    /* set the Undo, Redo, Insert, and WordWrap menu items */
  391.    wStatus = (UINT)edGetUndoState(hWnd);
  392.    if (!wStatus)
  393.       wStatus = MF_GRAYED;
  394.    else
  395.       wStatus = MF_ENABLED;
  396.    EnableMenuItem(hCurrentMenu, IDM_EDITUNDO, wStatus);
  397.  
  398.    wStatus = (UINT)edGetRedoState(hWnd);
  399.    if (!wStatus)
  400.       wStatus = MF_GRAYED;
  401.    else
  402.       wStatus = MF_ENABLED;
  403.    EnableMenuItem(hCurrentMenu, IDM_EDITREDO, wStatus);
  404.  
  405.    wStatus = (UINT)edGetWordWrapState(hWnd);
  406.    if (!wStatus)
  407.       wStatus = MF_UNCHECKED;
  408.    else
  409.       wStatus = MF_CHECKED;
  410.    CheckMenuItem (hCurrentMenu, IDM_EDITTOGGLEWRAP, MF_BYCOMMAND|wStatus);
  411.  
  412.    wStatus = (UINT)edGetInsertState(hWnd);
  413.    if (!wStatus)
  414.       wStatus = MF_UNCHECKED;
  415.    else
  416.       wStatus = MF_CHECKED;
  417.    CheckMenuItem (hCurrentMenu, IDM_EDITTOGGLEINS, MF_BYCOMMAND|wStatus);
  418.  
  419.    return TRUE;   /* we handled it, don't return 0 */
  420.    }
  421.